<?php

//$benutzer = "root";
//$passwort = "";
//$dbname = "archiv_bracher";
//mysql_connect("localhost", $benutzer, $passwort); mysql_select_db($Abacus);
//$insert = "REPLACE INTO Abacus (AbacusNr, AbacusName) values ('1', 'Max')";
//$db = mysql_query("$insert");
//mysql_close();

// Passing the XML
//$data = file_get_contents('AbaProj_CRM_short.xml');
//$projects = SimpleXMLElement($data);


global $element, $entry;
$entry = array();

function openElement( $parser, $tagName)
{
        global $element;
        $element = $tagName;
}

function cData( $parser, $data )
{
        global $element, $entry;
       
        if( ( $element != 'entry' ) && ( $element != 'data' ) )
                $entry[$element] .= $data;
}

function closeElement( $parser, $tagName )
{
        global $entry;

        if( $tagName == 'entry' )
        {
                // prepare sql query
                while( list( $key, $value ) = each( $entry ) )
                {
                        $AbacusNr .= "$ProjNr, ";
                        $AbacusName .=  "'$ProjName', ";
                }
               

                // remove all non-visible characters except SP, LF and CR.
                //$AbacusName = preg_replace( '/[^\x20-\xFF\x0A\x0D]/', '', $values );
               
                // build sql query
                $query = "REPLACE INTO Abacus ($AbacusNr,$AbacusName) VALUES ($ProjNr, $ProjName)";
                print "$query
\n";
                 
                // insert data
                $result = mysql_query( $query )
                        or die( "DB ERROR: query failed: $query" );

                // empty array
                $entry = array();
        }
}

// open connection and select database
$link = mysql_connect( 'localhost', 'root', '' )
         or die( 'DB ERROR: could not connect to database!' );

mysql_select_db( 'archiv_bracher' ) or die( 'DB ERROR: could not select database' );

// create instance of xml parser
$parser = xml_parser_create();

// do not uppercase all element identifiers
//xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );

// element handlers
xml_set_element_handler( $parser, 'openElement', 'closeElement' );

// cdata handler
xml_set_character_data_handler( $parser, 'cData' );

// open data file
if (! ( $fp = fopen( 'xml/file.xml', 'r' ) ) )
        die( 'ERROR: could not open file!' );


// read data line by line
while( !feof( $fp ) )
{
        // parse data line by line (without trailing \n)
        if( !xml_parse( $parser, chop( fgets( $fp ) ) ) )
                die(  'XML ERROR: ' .
                        xml_error_string( xml_get_error_code( $parser ) ) . ' in line ' .
                        xml_get_current_line_number( $parser )
                );
}

// close data file
fclose( $fp );

// remove instance of xml parser
xml_parser_free( $parser );

// close database connection
mysql_close( $link );




?>
